package com.cjk.console.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.cjk.console.enums.EventType;
import com.cjk.console.model.DeviceInfo;
import com.cjk.console.model.MqttMessage;
import com.cjk.console.request.DeviceRequest;
import com.cjk.foundation.commons.BusinessException;
import com.cjk.foundation.commons.response.Response;
import com.cjk.foundation.dao.ChannelDetailDao;
import com.cjk.foundation.dao.DeviceDao;
import com.cjk.foundation.dao.DeviceMgtLogDao;
import com.cjk.foundation.entity.ChannelDetailEntity;
import com.cjk.foundation.entity.DeviceEntity;
import com.cjk.foundation.enums.ConfStatus;
import com.cjk.foundation.enums.DeviceMgtType;
import com.cjk.foundation.enums.DeviceStatus;

@Service
public class DeviceOperateServiceImpl implements DeviceOperateService {

	@Autowired
	private ChannelDetailDao channelDao;

	@Autowired
	private DeviceDao deviceDao;

	@Autowired
	private DeviceMgtLogDao deviceMgtLogDao;

	@Autowired
	private MqttService mqttService;

	@Transactional
	@Override
	public Response reboot(DeviceRequest request, Integer userId) {
		if (CollectionUtils.isEmpty(request.getSns())) {
			throw new BusinessException("请先选择设备");
		}
		List<DeviceEntity> deviceEntities = deviceDao.selectListBySns(userId, request.getSns());
		for (DeviceEntity deviceEntity : deviceEntities) {
			// 设备下线
			DeviceEntity updateDeviceEntity = new DeviceEntity();
			updateDeviceEntity.setSn(deviceEntity.getSn());
			updateDeviceEntity.setStatus(DeviceStatus.OFFLINE);
			deviceDao.update(updateDeviceEntity);
			// 记录设备履历
			deviceMgtLogDao.insert(userId, deviceEntity.getGroupId(), deviceEntity.getSn(), DeviceMgtType.REBOOT,
					DeviceMgtType.REBOOT.getLabel());
			// 下发重启
			MqttMessage mqttMessage = new MqttMessage();
			mqttMessage.setEventType(EventType.REBOOT);
			mqttMessage.setSn(deviceEntity.getSn());
			mqttService.publish(deviceEntity.getSn(), mqttMessage);
		}
		return Response.success();
	}

	@Transactional
	@Override
	public Response configDevice(DeviceRequest request, Integer userId) {
		DeviceEntity deviceEntity = deviceDao.selectBySn(request.getSn(), userId);
		if (deviceEntity == null) {
			throw new BusinessException("设备不存在");
		}
		if (deviceEntity.getUserId() != userId) {
			throw new BusinessException("您没有该设备的访问权限");
		}
		// 修改设备配置
		DeviceEntity updateDeviceEntity = new DeviceEntity();
		updateDeviceEntity.setSn(deviceEntity.getSn());
		updateDeviceEntity.setAliasName(request.getAliasName());
		updateDeviceEntity.setPeriodic(request.getPeriodic());
		deviceDao.update(updateDeviceEntity);
		// 记录设备履历
		String desc = DeviceMgtType.DEVICE_CONFIG.getLabel() + "：";
		if (StringUtils.isNoneBlank(request.getAliasName())) {
			desc += "设备别名：" + request.getAliasName() + ";";
		}
		if (request.getPeriodic() != null) {
			desc += "周期时间：" + request.getPeriodic() + ";";
		}
		deviceMgtLogDao.insert(userId, deviceEntity.getGroupId(), deviceEntity.getSn(), DeviceMgtType.DEVICE_CONFIG,
				desc);
		// 下发配置
		MqttMessage mqttMessage = new MqttMessage();
		mqttMessage.setEventType(EventType.CONFIG_DEVICE);
		mqttMessage.setSn(deviceEntity.getSn());
		DeviceInfo deviceInfo = new DeviceInfo();
		deviceInfo.setAliasName(request.getAliasName());
		deviceInfo.setPeriodic(request.getPeriodic());
		mqttService.publish(deviceEntity.getSn(), mqttMessage);
		return Response.success();
	}

	@Transactional
	@Override
	public Response configChannel(DeviceEntity deviceEntity) {
		List<ChannelDetailEntity> channelEntities = channelDao.selectListByCondition(deviceEntity.getUserId(), null, null);
		if (!CollectionUtils.isEmpty(channelEntities)) {
			DeviceEntity updateDeviceEntity = new DeviceEntity();
			updateDeviceEntity.setSn(deviceEntity.getSn());
			updateDeviceEntity.setConfStatus(ConfStatus.RUNNING);
			deviceDao.update(updateDeviceEntity);
			// 记录设备履历
			deviceMgtLogDao.insert(deviceEntity.getUserId(), deviceEntity.getGroupId(), deviceEntity.getSn(),
					DeviceMgtType.CHANNEL_CONFIG, DeviceMgtType.CHANNEL_CONFIG.getLabel());
			// 下发配置
			MqttMessage mqttMessage = new MqttMessage();
			mqttMessage.setEventType(EventType.CONFIG_TV);
			mqttMessage.setChannelList(channelEntities);
			mqttMessage.setSn(deviceEntity.getSn());
			mqttService.publish(deviceEntity.getSn(), mqttMessage);
		}
		return Response.success();
	}

}
